<?php
define('IN',1);

require_once 'common.inc.php';

if(count($_G)<1)
{
	Header("Location: user.php");
    exit;
}

$cid       = (int)$_GET['cid'];
$page_no      = (int)$_GET['page_no'] > 0 ? $_GET['page_no'] : 1;
$_GET['page_no'] = 'TPT';
$pid       = (int)$_GET['pid'];
$keyword   = urldecode($_GET['keyword']);

if (!$pid) 
{
	$currItem = '';
	
	//当前类别的所有子类
    $product_list = array();
    foreach ($product_class as $item) 
	{  
        if ($item['parent_id'] == $cid) 
		{
            $product_list[] = $item;
        }
		if ($item['class_id'] == $cid) 
		{
            $currItem = $item;
        }
    }
	
    if (!$product_list) 
	{
		//当前分类的父级ID
        $ppid = 0;
        foreach ($product_class as $item) 
		{
            if ($item['class_id'] == $cid) 
			{
                $ppid = $item['parent_id'];
                break;
            }
        }
		
		//当前分类的父级 下面的所有子级类别， 即 当前节点的所有兄弟节点
        $product_list = array();
        foreach ($product_class as $item) 
		{
            if ($item['parent_id'] == $ppid) 
			{
                $product_list[] = $item;
            }
        }
    }
	
	
    $class_id_list = array(0);
    $list_where = $count_where = 'where 1';
    if ($cid) 
	{
        $class_id_list = array(0,$cid);
        # 获取包括当前分类在内的所有下级分类
        foreach ($product_class as $item) 
		{
            $path = ",{$item['path']},";
            if (strpos($path,",{$cid},")) 
			{
                $class_id_list[] = $item['class_id'];
            }
        }
        $list_where .= ' and p.cid in ('.implode(',',$class_id_list).')';
        $count_where .= ' and cid in ('.implode(',',$class_id_list).')';
    }
	if ($keyword) 
	{
        $list_where .= " and p.name like '%{$keyword}%'";
        $count_where .= " and name like '%{$keyword}%'";
    }
    
	$total = $_ENV['db']->fetchCol("SELECT count(1) FROM product {$count_where}");
    $pager = new class_pager();
    $pager->setConfig($page_no,$total,12,8);
    $pager->setUrl("product.php?cid={$cid}&keyword={$keyword}&page_no=tpt",'tpt');
	$template->pager = $pager->output('page_num');
    $products = $_ENV['db']->fetchAll("SELECT p.*,i.*,p.product_id FROM product as p left join product_image as i on p.default_imagehash=i.hash {$list_where} LIMIT ".$pager->getLimit()." OFFSET ".$pager->getOffset());
    $template->product_list = $product_list;
    $template->cid = $cid;
    $template->page_no = $page_no;
    
	$template->currItem = $currItem;
    $template->products = $products;
    showMainFrontPage('front/product_list');
} 
else 
{
    $product = $_ENV['db']->fetch("SELECT * FROM product as p left join product_image as i on p.default_imagehash=i.hash where p.product_id = '{$pid}'");
    if (!$product) exit('该商品不存在');

    $template->product = $product;

    # 整理分类
    $ppid = 0;
    foreach ($product_class as $item) {
        if ($item['class_id'] == $product['cid']) {
            $ppid = $item['parent_id'];
            break;
        }
    }
    $product_list = array();
    foreach ($product_class as $item) {
        if ($item['parent_id'] == $ppid) {
            $product_list[] = $item;
        }
    }
    $template->product_list = $product_list;

    $template->addScript('product');
    showMainFrontPage('front/product_detail');
}